//
//  OnePicFromLibrary.m
//  选中的每一个图片
//
//  Created by Yangfan on 15/2/4.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "OnePicFromLibrary.h"

@implementation OnePicFromLibrary

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initResource];
    }
    return self;
}

// 初始化每一个添加图片的样式
- (void)initResource {
    self.backgroundColor = [UIColor clearColor];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 64, 64)];
    _imageView.contentMode=UIViewContentModeScaleAspectFill;
    _imageView.clipsToBounds=YES;
//    _imageView.layer.borderWidth = 2.0f;
//    _imageView.layer.borderColor = [UIColor whiteColor].CGColor;
    [self addSubview:_imageView];
    
    // 图片上面的删除按钮
    _deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(54, 0, 20, 20)];
    [_deleteButton.layer setCornerRadius:12.5f];
    [_deleteButton setImage:[UIImage imageNamed:@"deletePic"] forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(deleteButtonAction:)
            forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_deleteButton];
}

// 添加每一个用户点击了的图片
- (void)setImageAsset:(ALAsset *)asset index:(NSInteger)index {
    _asset = asset;
    _index = index;
    dispatch_async(dispatch_get_main_queue(), ^{
        [_imageView setImage:[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage]];
    });
}

// 删除选择了的图片操作，使用代理完成
- (void)deleteButtonAction:(id)sender {
    if(_delegate && [_delegate respondsToSelector:@selector(responseToDelete:)]) {
        [_delegate responseToDelete:self];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
